﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;
using System.Data;
using System.Text;
using System.IO;

namespace He.Web.m
{
    public partial class index : Front
    {
        string theme;
        string templateDir;
        TemplateHelper th;
        Model.wx_config wx_config;
        protected void Page_Load(object sender, EventArgs e)
        {
            wx_config = new BLL.wx_config().LoadConfig();
            string templateName = siteInfo.Template;
            theme = string.Format("{0}templates/{1}/", webset.web_path, templateName);
            templateDir = PathHelper.MapPath(string.Format("~/templates/{0}/", templateName));

            this.th = new TemplateHelper(templateDir);

            PutCommon();  //加载公共部分

            DisplayPage();
        }

        //首页
        private void DisplayPage()
        {
            string page = InputUtil.Get("p", "index");
            switch (page)
            {
                case "category":
                    LoadCategory();
                    break;
                case "page":
                    LoadPage();
                    break;
                case "list":
                    LoadList();
                    break;
                case "show":
                    LoadShow();
                    break;
                case "feedback":
                    LoadFeedback();
                    break;
                default:
                    LoadDefault();
                    break;
            }
        }

        //加载首页
        private void LoadDefault()
        {
            if (!File.Exists(templateDir + "index.html"))
            {
                Response.Write("模板" + siteInfo.Template + "的文件不存在，请先上传相应模板文件");
                Response.End();
            }

            //标题
            th.Put("title", siteInfo.WebTitle);
            //Meta信息
            th.Put("metainfo", AddMetaInfo("", ""));
            Display("index.html");
        }
        //加载分类页
        private void LoadCategory()
        {
            string name = InputUtil.Get("name", "mabout");

            BLL.category bll = new BLL.category();

            //栏目
            Model.channel channel = new BLL.channel().GetModel(name);

            //分类
            List<Model.category> cateList = bll.DataTableToList(bll.GetList("channel_id=" + channel.id + " order by sort_id asc,id desc").Tables[0]);

            th.Put("channel", channel);
            th.Put("cateList", cateList);

            //标题
            th.Put("title", Mtitle(channel.title));
            //Meta信息
            th.Put("metainfo", AddMetaInfo(channel.seo_keywords, channel.seo_desc));

            Display("category.html");
        }
        //加载内容页
        private void LoadPage()
        {
            string name = GetString("name");

            BLL.article bll = new BLL.article();

            //栏目
            Model.channel channel = new BLL.channel().GetModel(name);

            int channel_id = channel.id;

            //分类
            DataTable cateList = new BLL.article().GetList(channel_id).Tables[0];

            int id = GetInt("id", bll.GetFirstId(name));

            //内容
            Model.article model = bll.GetModel(id);

            th.Put("channel", channel);
            th.Put("cateList", cateList);
            th.Put("model", model);

            //标题
            th.Put("title", Mtitle(model.title));
            //Meta信息
            th.Put("metainfo", AddMetaInfo(model.seo_keywords, StringHelper.CutString(model.seo_desc, 250)));

            Display("content.html");
        }

        //加载列表页
        private void LoadList()
        {
            string name = InputUtil.Get("name", "mproduct");
            int category_id = GetInt("category_id", 0);
            int page = GetInt("page", 0);
            BLL.article abll = new BLL.article();

            //栏目
            Model.channel channel = new He.BLL.channel().GetModel(name);
            //分类
            DataTable cateList = new BLL.category().GetList(0, channel.id);
            //列表
            string strWhere = CombSqlTxt(channel.id, category_id);
            List<Model.article> list = new List<Model.article>();
            list = abll.DataTableToList(abll.GetPageList(channel.page_size, page, strWhere, channel.order_by).Tables[0]);
            //分页
            int rcount = abll.GetRecordCount(strWhere);
            string pager = HtmlHelper.OutPageList(channel.page_size, page, rcount, HtmlHelper.url(name, category_id));

            #region SEO信息
            string crumb_txt = channel.title;
            string webtitle = Mtitle(channel.title);
            string seo_keywords = channel.seo_keywords;
            string seo_desc = channel.seo_desc;
            if (category_id > 0)
            {
                He.Model.category cmodel = new He.BLL.category().GetModel(category_id);
                crumb_txt = cmodel.title;
                webtitle = Mtitle(cmodel.title);
                seo_keywords = cmodel.seo_keywords;
                seo_desc = cmodel.seo_desc;
            }
            #endregion

            th.Put("channel", channel);
            th.Put("cateList", cateList);
            th.Put("pager", pager);
            th.Put("cateList", cateList);
            th.Put("list", list);
            th.Put("crumb_txt", crumb_txt);
            //标题
            th.Put("title", webtitle);
            //Meta信息
            th.Put("metainfo", AddMetaInfo(seo_keywords, seo_desc));
            Display("list.html");
        }

        #region 加载详细页===================================
        private void LoadShow()
        {
            int id = GetInt("id", 0);

            BLL.channel channelbll = new BLL.channel();
            BLL.category catebll = new BLL.category();
            BLL.article abll = new BLL.article();
            Model.channel channel = new Model.channel();
            Model.category category = new Model.category();
            Model.article amodel = new Model.article();

            int prevId = 0;
            int nextId = 0;
            string prevHtml = "没有了";
            string nextHtml = "没有了";

            abll.UpdateField(id, "click=click+1");
            amodel = abll.GetModel(id);
            category = catebll.GetModel(amodel.category_id);
            channel = channelbll.GetModel(category.channel_id);

            prevId = abll.GetPrevId(category.channel_id, id);
            nextId = abll.GetNextId(category.channel_id, id);

            if (prevId > 0)
            {
                prevHtml = string.Format("<a href=\"?p=show&id={0}\">{1}</a>", prevId, abll.GetModel(prevId).title);
            }
            if (nextId > 0)
            {
                nextHtml = string.Format("<a href=\"?p=show&id={0}\">{1}</a>", nextId, abll.GetModel(nextId).title);
            }
            th.Put("title", WebTitle(category.title, amodel.title));
            th.Put("metainfo", AddMetaInfo(amodel.seo_keywords, amodel.seo_desc));
            th.Put("model", amodel);

            th.Put("channel", channel);
            th.Put("prevId", prevId);
            th.Put("nextId", nextId);
            th.Put("category", category);
            th.Put("crumb_txt", category.title);
            th.Put("prevHtml", prevHtml);
            th.Put("nextHtml", nextHtml);

            Display("show.html");
        }
        #endregion

        #region 加载留言页===================================
        private void LoadFeedback()
        {
            int page_size = 5;
            int page = GetInt("page", 0);

            BLL.feedback bll = new BLL.feedback();
            string strWhere = string.Format("status={0} and pos_mark='feedback' and lang='mobile'", (int)Enums.AuditStatus.已审核);
            List<Model.feedback> list = bll.DataTableToList(bll.GetPageList(page_size, page, strWhere, "id desc").Tables[0]);

            int rcount = bll.GetRecordCount(strWhere);
            string pager = HtmlHelper.OutPageList(page_size, page, rcount, "index.aspx?p=plug&page=__id__&act=feedback");
            th.Put("list", list);
            th.Put("pager", pager);

            th.Put("title", WebTitle());
            th.Put("metainfo", AddMetaInfo("", ""));

            Display("feedback.html");
        }
        #endregion

        //加载公共部分
        private void PutCommon()
        {
            //公共变量
            th.Put("theme", theme);
            th.Put("wxconfig", wx_config);
            th.Put("info", siteInfo);
            th.Put("config", siteConfig);
            //公共类
            th.Put("this", this);
            th.Put("string", new StringHelper());
            th.Put("html", new HtmlHelper());
            th.Put("biz", new BizHelper());
        }

        private void Display(string templateFile)
        {
            string templatePath = templateDir + templateFile;
            HttpContext.Current.Response.Write(th.BuildString(templateFile));
        }

        #region 网站标题信息

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string Mtitle()
        {
            return siteInfo.WebName;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="classTitle"></param>
        /// <returns></returns>
        public string Mtitle(string category)
        {
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append(category);
            strTxt.Append(" - ");
            strTxt.Append(siteInfo.WebName);
            return strTxt.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="classTitle"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public string Mtitle(string category, string title)
        {
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append(title);
            strTxt.Append(" - ");
            strTxt.Append(category);
            strTxt.Append(" - ");
            strTxt.Append(siteInfo.WebName);
            return strTxt.ToString();
        }

        #endregion

    }
}